import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../controllers/login_controller.dart';
import '../widgets/auth_header.dart';
import '../../shared/widgets/forms/app_text_field.dart';
import '../../shared/widgets/buttons/app_button.dart';

/// 登录视图
class LoginView extends GetView<LoginController> {
  const LoginView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: SingleChildScrollView(
          padding: const EdgeInsets.all(24.0),
          child: Form(
            key: controller.formKey,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                // 头部
                const AuthHeader(
                  title: '欢迎回来!',
                  subtitle: '请输入您的凭证继续',
                ),
                
                const SizedBox(height: 40),
                
                // 邮箱输入框
                AppTextField(
                  controller: controller.emailController,
                  labelText: '邮箱',
                  hintText: '请输入您的邮箱地址',
                  keyboardType: TextInputType.emailAddress,
                  prefixIcon: const Icon(Icons.email_outlined),
                  validator: controller.validateEmail,
                ),
                
                const SizedBox(height: 20),
                
                // 密码输入框
                Obx(() => AppTextField(
                  controller: controller.passwordController,
                  labelText: '密码',
                  hintText: '请输入您的密码',
                  keyboardType: TextInputType.visiblePassword,
                  obscureText: !controller.isPasswordVisible.value,
                  prefixIcon: const Icon(Icons.lock_outline),
                  suffixIcon: IconButton(
                    icon: Icon(
                      controller.isPasswordVisible.value
                          ? Icons.visibility_off_outlined
                          : Icons.visibility_outlined,
                    ),
                    onPressed: controller.togglePasswordVisibility,
                  ),
                  validator: controller.validatePassword,
                )),
                
                const SizedBox(height: 16),
                
                // 忘记密码链接
                Align(
                  alignment: Alignment.centerRight,
                  child: TextButton(
                    onPressed: controller.navigateToForgotPassword,
                    child: const Text('忘记密码?'),
                  ),
                ),
                
                const SizedBox(height: 30),
                
                // 登录按钮
                Obx(() => AppButton(
                  text: '登录',
                  onPressed: controller.login,
                  isLoading: controller.isLoading.value,
                )),
                
                const SizedBox(height: 20),
                
                // 分隔线
                Row(
                  children: [
                    Expanded(child: Divider(color: Colors.grey[300])),
                    Padding(
                      padding: const EdgeInsets.symmetric(horizontal: 16),
                      child: Text('或', style: TextStyle(color: Colors.grey[600])),
                    ),
                    Expanded(child: Divider(color: Colors.grey[300])),
                  ],
                ),
                
                const SizedBox(height: 20),
                
                // 其他登录方式（示例）
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    _buildSocialLoginButton(Icons.android, Colors.green),
                    const SizedBox(width: 20),
                    _buildSocialLoginButton(Icons.apple, Colors.black),
                  ],
                ),
                
                const SizedBox(height: 40),
                
                // 注册链接
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    const Text('还没有账户?'),
                    TextButton(
                      onPressed: controller.navigateToRegister,
                      child: const Text('立即注册'),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
  
  /// 构建社交登录按钮
  Widget _buildSocialLoginButton(IconData icon, Color color) {
    return IconButton(
      icon: Icon(icon, size: 36, color: color),
      onPressed: () {
        Get.snackbar('提示', '社交登录功能正在开发中');
      },
      style: IconButton.styleFrom(
        padding: const EdgeInsets.all(12),
        backgroundColor: Colors.grey[100],
        shape: CircleBorder(side: BorderSide(color: Colors.grey[300]!)),
      ),
    );
  }
} 